import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../common/style/colors.dart';

class EditText extends StatefulWidget {
  final TextInputType? keyboardType;
  final int? maxLines;
  final int? maxLength;
  final String? hintText;
  final TextStyle? hintStyle;
  final Icon? deleteIcon;
  final InputBorder? inputBorder;
  final Widget? prefixIcon;
  final Widget? icon;
  final TextStyle? textStyle;
  final bool hasDeleteIcon;
  final bool obscureText;
  final List<TextInputFormatter>? inputFormatters;
  final ValueChanged<String>? onChanged;

  EditText(
      {Key? key,
      this.keyboardType,
      this.maxLines,
      this.maxLength,
      this.hintText,
      this.hintStyle,
      this.deleteIcon,
      this.inputBorder,
      this.prefixIcon,
      this.icon,
      this.textStyle,
      this.hasDeleteIcon = true,
      this.onChanged,
      this.obscureText = false,
      this.inputFormatters})
      : super(key: key);

  @override
  _EditTextState createState() {
    return _EditTextState();
  }
}

class _EditTextState extends State<EditText> {
  String _inputText = "";
  bool _hasdeleteIcon = false;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    TextEditingController controller = TextEditingController.fromValue(TextEditingValue(
        text: _inputText,
        selection:
            TextSelection.fromPosition(TextPosition(affinity: TextAffinity.downstream, offset: _inputText.length))));
    TextField textField = TextField(
      controller: controller,
      decoration: InputDecoration(
        hintStyle: widget.hintStyle ?? const TextStyle(fontSize: 15, color: AppColors.colorTextSub),
        counterStyle: const TextStyle(color: Colors.white),
        hintText: widget.hintText,

        focusedBorder: widget.inputBorder ??
            const UnderlineInputBorder(borderSide: BorderSide(color: AppColors.colorF7, width: 1)),
        // border: widget.inputBorder ?? const UnderlineInputBorder(borderSide: BorderSide(color: AppColors.red,width: 12)),
        enabledBorder: widget.inputBorder ??
            const UnderlineInputBorder(borderSide: BorderSide(color: AppColors.colorF7, width: 1)),
        disabledBorder: widget.inputBorder ?? const UnderlineInputBorder(borderSide: BorderSide(color: AppColors.red)),

        fillColor: Colors.transparent,
        filled: true,
        icon: widget.icon,
        prefixIcon: widget.prefixIcon,
        suffixIcon: widget.hasDeleteIcon == true&&_hasdeleteIcon
            ? SizedBox(
                width: 20.0,
                height: 20.0,
                child: IconButton(
                  alignment: Alignment.center,
                  padding: const EdgeInsets.all(0.0),
                  iconSize: 16.0,
                  icon: widget.deleteIcon ??
                      const Icon(
                        Icons.cancel,
                        color: AppColors.colorB6,
                      ),
                  onPressed: () {
                    setState(() {
                      _inputText = "";
                      _hasdeleteIcon = (_inputText.isNotEmpty);
                      if (widget.onChanged != null) widget.onChanged!(_inputText);
                    });
                  },
                ),
              )
            : const SizedBox(),
      ),
      onChanged: (str) {
        setState(() {
          _inputText = str;
          _hasdeleteIcon = (_inputText.isNotEmpty);
          if (widget.onChanged != null) widget.onChanged!(_inputText);
        });
      },
      keyboardType: widget.keyboardType,
      maxLength: widget.maxLength,
      maxLines: widget.maxLines,
      inputFormatters: widget.inputFormatters,
      style: widget.textStyle ?? const TextStyle(fontSize: 14, color: AppColors.color33),
      obscureText: widget.obscureText,
    );
    return textField;
  }
}
